<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Linux -->
<!-- Start book Linux -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>shell编程 | Linux</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="chris Jiang">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    
    <link rel="prev" href="./14/section04.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="15"
        data-chapter-title="shell编程"
        data-filepath="chapter15.md"
        data-basepath="."
        data-revision="Mon Sep 30 2019 00:13:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Linux入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01/section01.html">
            
                
                    <a href="./01/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        操作系统介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01/section02.html">
            
                
                    <a href="./01/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Linux系统介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01/section03.html">
            
                
                    <a href="./01/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        使用虚拟机安装Ubuntu
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="01/section04.html">
            
                
                    <a href="./01/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        使用U盘安装双系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="01/section05.html">
            
                
                    <a href="./01/section05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        配置Linux系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="01/section06.html">
            
                
                    <a href="./01/section06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        Linux启动流程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="01/section07.html">
            
                
                    <a href="./01/section07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        远程连接Linux
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        软件安装相关指令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        文件系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        vim编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        目录操作的常见指令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        python、pip和ipython的安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        虚拟环境管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="./chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        用户管理相关指令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="chapter09.html">
            
                
                    <a href="./chapter09.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        权限管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10" data-path="chapter10.html">
            
                
                    <a href="./chapter10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        压缩解压命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="11" data-path="chapter11.html">
            
                
                    <a href="./chapter11.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        服务监听
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="chapter12.html">
            
                
                    <a href="./chapter12.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        管道和重定向
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="13" data-path="chapter13.html">
            
                
                    <a href="./chapter13.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>13.</b>
                        
                        Nginx的安装和配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14" data-path="chapter14.html">
            
                
                    <a href="./chapter14.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.</b>
                        
                        Git的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="14.1" data-path="14/section01.html">
            
                
                    <a href="./14/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.1.</b>
                        
                        git本地和远端管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.2" data-path="14/section02.html">
            
                
                    <a href="./14/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.2.</b>
                        
                        冲突的介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.3" data-path="14/section03.html">
            
                
                    <a href="./14/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.3.</b>
                        
                        分支操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="14.4" data-path="14/section04.html">
            
                
                    <a href="./14/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>14.4.</b>
                        
                        远端服务器的SSH连接
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="15" data-path="chapter15.html">
            
                
                    <a href="./chapter15.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>15.</b>
                        
                        shell编程
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Linux</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="shell&#x7F16;&#x7A0B;&#x4E0E;&#x8FD0;&#x7EF4;">Shell&#x7F16;&#x7A0B;&#x4E0E;&#x8FD0;&#x7EF4;</h1>
<h2 id="&#x4E00;&#x3001;shell-&#x811A;&#x672C;&#x6982;&#x8FF0;">&#x4E00;&#x3001;Shell &#x811A;&#x672C;&#x6982;&#x8FF0;</h2>
<p>&#x901A;&#x8FC7; Shell &#x4E2D;&#x7684;&#x5404;&#x79CD;&#x547D;&#x4EE4;, &#x5F00;&#x53D1;&#x8005;&#x548C;&#x8FD0;&#x7EF4;&#x4EBA;&#x5458;&#x53EF;&#x4EE5;&#x5BF9;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x7EF4;&#x62A4;&#x5DE5;&#x4F5C;&#x3002;</p>
<p>&#x4F46;&#x6BCF;&#x6B21;&#x90FD;&#x624B;&#x52A8;&#x8F93;&#x5165;&#x547D;&#x4EE4;, &#x5DE5;&#x4F5C;&#x6548;&#x7387;&#x592A;&#x4F4E;, &#x800C;&#x4E14;&#x5F88;&#x5BB9;&#x6613;&#x51FA;&#x9519;, &#x5C24;&#x5176;&#x662F;&#x9700;&#x8981;&#x7EF4;&#x62A4;&#x5927;&#x91CF;&#x670D;&#x52A1;&#x5668;&#x65F6;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x80FD;&#x591F;&#x5BF9;&#x670D;&#x52A1;&#x5668;&#x6279;&#x91CF;&#x6267;&#x884C;&#x64CD;&#x4F5C;, &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x9700;&#x8981;&#x6267;&#x884C;&#x7684;&#x547D;&#x4EE4;&#x5199;&#x5165;&#x6587;&#x4EF6;, &#x6279;&#x91CF;&#x6267;&#x884C;, &#x8FD9;&#x79CD;&#x6587;&#x4EF6;&#x4FBF;&#x662F; Shell &#x811A;&#x672C;&#x3002;</p>
<p>Shell &#x811A;&#x672C;&#x4E00;&#x822C;&#x662F;&#x4EE5; <code>.sh</code> &#x7ED3;&#x5C3E;&#x7684;&#x6587;&#x672C;&#x6587;&#x4EF6;, &#x5F53;&#x7136;, &#x4E5F;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x6269;&#x5C55;&#x540D;&#x3002;</p>
<h2 id="&#x4E8C;&#x3001;shell-&#x811A;&#x672C;&#x9996;&#x884C;">&#x4E8C;&#x3001;Shell &#x811A;&#x672C;&#x9996;&#x884C;</h2>
<p>&#x811A;&#x672C;&#x6587;&#x4EF6;&#x7B2C;&#x4E00;&#x884C;&#x901A;&#x8FC7;&#x6CE8;&#x91CA;&#x7684;&#x65B9;&#x5F0F;&#x6307;&#x660E;&#x6267;&#x884C;&#x811A;&#x672C;&#x7684;&#x7A0B;&#x5E8F;&#x3002;</p>
<p>&#x5728; Shell &#x811A;&#x672C;&#x4E2D;, <code>#</code> &#x5F00;&#x5934;&#x7684;&#x6587;&#x672C;&#x662F;&#x6CE8;&#x91CA;, &#x4F46;&#x7B2C;&#x4E00;&#x53E5; <code>#!</code> &#x5F00;&#x5934;&#x7684;&#x8FD9;&#x53E5;&#x8BDD;&#x6BD4;&#x8F83;&#x7279;&#x6B8A;, &#x4ED6;&#x4F1A;&#x544A;&#x8BC9; Shell &#x5E94;&#x8BE5;&#x4F7F;&#x7528;&#x54EA;&#x4E2A;&#x7A0B;&#x5E8F;&#x6765;&#x6267;&#x884C;&#x5F53;&#x524D;&#x811A;&#x672C;&#x3002;</p>
<p>&#x5E38;&#x89C1;&#x65B9;&#x5F0F;&#x6709;:</p>
<ul>
<li><code>#!/bin/sh</code></li>
<li><code>#!/bin/bash</code></li>
<li><code>#!/usr/bin/env bash</code></li>
</ul>
<p>Python &#x811A;&#x672C;&#x7684;&#x7B2C;&#x4E00;&#x53E5;&#x4E00;&#x822C;&#x662F; <code>#!/usr/bin/env python</code></p>
<h2 id="&#x4E09;&#x3001;&#x7B2C;&#x4E00;&#x4E2A;&#x811A;&#x672C;">&#x4E09;&#x3001;&#x7B2C;&#x4E00;&#x4E2A;&#x811A;&#x672C;</h2>
<ol>
<li>&#x521B;&#x5EFA; cpu-count.sh &#x6587;&#x4EF6;</li>
<li><p>&#x5C06;&#x4E0B;&#x9762;&#x6587;&#x672C;&#x5199;&#x5165; cpu-count.sh &#x4E2D;</p>
<pre><code class="lang-bash"> <span class="hljs-comment">#!/bin/bash</span>

 <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Hello&quot;</span>
 <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;I am `whoami`&quot;</span>
 <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;I love Linux&quot;</span>
 <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;The CPU in my PC has `cat /proc/cpuinfo |grep -c processor` cores&quot;</span>
 <span class="hljs-built_in">exit</span> <span class="hljs-number">0</span>
</code></pre>
</li>
<li>&#x6267;&#x884C; <code>chmod a+x cpu-count.sh</code> &#x5BF9;&#x811A;&#x672C;&#x6388;&#x4E88;&#x53EF;&#x6267;&#x884C;&#x6743;&#x9650;</li>
<li>&#x8F93;&#x5165; <code>./cpu-count.sh</code> &#x6267;&#x884C;&#x811A;&#x672C;</li>
<li><p>&#x67E5;&#x770B;&#x811A;&#x672C;&#x7684;&#x9000;&#x51FA;&#x72B6;&#x6001;: <code>echo $?</code></p>
<ul>
<li>Linux &#x4E2D;&#x7684;&#x6240;&#x6709;&#x7A0B;&#x5E8F;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x540E;&#x90FD;&#x6709;&#x72B6;&#x6001;&#x7801;</li>
<li>&#x72B6;&#x6001;&#x7801;&#x4E3A; <strong>&#x96F6;</strong> &#x8868;&#x793A;&#x6B63;&#x5E38;, &#x72B6;&#x6001;&#x7801;&#x4E3A; <strong>&#x6B63;&#x6574;&#x6570;</strong> &#x4EE3;&#x8868;&#x5F02;&#x5E38;&#x9000;&#x51FA;</li>
</ul>
</li>
</ol>
<h2 id="&#x56DB;&#x3001;&#x53D8;&#x91CF;">&#x56DB;&#x3001;&#x53D8;&#x91CF;</h2>
<ol>
<li><p>&#x5B9A;&#x4E49;</p>
<p> &#x53D8;&#x91CF;&#x7684;&#x5B9A;&#x4E49;&#x4E0E;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x5DEE;&#x8DDD;&#x4E0D;&#x5927;, &#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#x8D4B;&#x503C;&#x524D;&#x540E;&#x6CA1;&#x6709;&#x7A7A;&#x683C;&#x3002;</p>
<pre><code class="lang-bash"> <span class="hljs-comment"># &#x53D8;&#x91CF;&#x5B9A;&#x4E49;: &#x7B49;&#x53F7;&#x524D;&#x540E;&#x6CA1;&#x6709;&#x7A7A;&#x683C;</span>
 a=<span class="hljs-number">12345</span>
 b=xyz
</code></pre>
</li>
<li><p>&#x4F7F;&#x7528;</p>
<p> &#x4F7F;&#x7528;&#x53D8;&#x91CF;&#x65F6;, &#x53D8;&#x91CF;&#x540D;&#x524D;&#x9762;&#x52A0;&#x4E0A; <code>$</code> &#x7B26;</p>
<pre><code class="lang-bash"> <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;---<span class="hljs-variable">$a</span>---\n===<span class="hljs-variable">$b</span>===&quot;</span>
 <span class="hljs-built_in">printf</span> <span class="hljs-string">&quot;---<span class="hljs-variable">$a</span>---\n===<span class="hljs-variable">$b</span>===\n&quot;</span>
</code></pre>
</li>
<li><p>&#x6CE8;&#x610F; &#x5F15;&#x53F7; &#x7684;&#x5DEE;&#x522B;</p>
<pre><code class="lang-bash"> <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;---<span class="hljs-variable">$a</span>---\n===<span class="hljs-variable">$b</span>===&quot;</span>
 <span class="hljs-built_in">echo</span> <span class="hljs-string">&apos;---$a---\n===$b===&apos;</span>
</code></pre>
</li>
</ol>
<ol>
<li><p>&#x5B9A;&#x4E49;&#x5F53;&#x524D;Shell&#x4E0B;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;</p>
<ol>
<li>&#x5B9A;&#x4E49;: <code>export ABC=9876543210123456789</code></li>
<li>&#x5B9A;&#x4E49;&#x5B8C;&#x540E;, &#x5728;&#x7EC8;&#x7AEF;&#x91CC;&#x7528; source &#x52A0;&#x8F7D;&#x811A;&#x672C;: <code>source ./test.sh</code></li>
</ol>
</li>
<li><p>&#x5E38;&#x7528;&#x7684;&#x7CFB;&#x7EDF;&#x73AF;&#x5883;&#x53D8;&#x91CF;</p>
<ul>
<li><code>$PATH</code>: &#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x76EE;&#x5F55;</li>
<li><code>$PWD</code>: &#x5F53;&#x524D;&#x76EE;&#x5F55;</li>
<li><code>$HOME</code>: &#x5BB6;&#x76EE;&#x5F55;</li>
<li><code>$USER</code>: &#x5F53;&#x524D;&#x7528;&#x6237;&#x540D;</li>
<li><code>$UID</code>: &#x5F53;&#x524D;&#x7528;&#x6237;&#x7684; uid</li>
</ul>
</li>
</ol>
<h2 id="&#x4E94;&#x3001;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x8F93;&#x5165;">&#x4E94;&#x3001;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x8F93;&#x5165;</h2>
<pre><code class="lang-bash"><span class="hljs-built_in">read</span> -p <span class="hljs-string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#xFF1A;&quot;</span> num
<span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x60A8;&#x8F93;&#x5165;&#x7684;&#x662F;&#xFF1A;<span class="hljs-variable">$num</span>&quot;</span>
</code></pre>
<h2 id="&#x516D;&#x3001;&#x811A;&#x672C;&#x91CC;&#x7684;&#x5F15;&#x53F7;">&#x516D;&#x3001;&#x811A;&#x672C;&#x91CC;&#x7684;&#x5F15;&#x53F7;</h2>
<p>&#x5728;shell&#x91CC;&#x652F;&#x6301;&#x4E09;&#x79CD;&#x5F15;&#x53F7;&#xFF0C;&#x5B83;&#x4EEC;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x4EE5;&#x53CA;&#x6548;&#x679C;&#x4F1A;&#x6709;&#x5DEE;&#x5F02;&#x3002;</p>
<ul>
<li><p>&#x53CD;&#x5F15;&#x53F7;:&#x91CC;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x4F1A;&#x88AB;&#x89E3;&#x6790;&#x6210;&#x4E3A;&#x811A;&#x672C;</p>
<pre><code class="lang-shell">echo `ls -a /home/chris`   # &#x6253;&#x5370; /home/chris&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x548C;&#x6587;&#x4EF6;&#x5939;
</code></pre>
</li>
<li><p>&#x53CC;&#x5F15;&#x53F7;&#xFF1A;&#x5B83;&#x91CC;&#x9762;&#x7684;&#x5927;&#x90E8;&#x5206;&#x5185;&#x5BB9;&#x4F1A;&#x522B;&#x539F;&#x6837;&#x663E;&#x793A;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x4F1A;&#x88AB;&#x89E3;&#x6790;&#x3002;</p>
<pre><code class="lang-shell">name=zhangsan
echo &quot;&#x5927;&#x5BB6;&#x597D;&#xFF0C;&#x6211;&#x662F;$name&quot;  # &#x5927;&#x5BB6;&#x597D;&#xFF0C;&#x6211;&#x662F;zhangsan
</code></pre>
</li>
<li><p>&#x5355;&#x5F15;&#x53F7;&#xFF1A;&#x5B83;&#x91CC;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x4F1A;&#x5B8C;&#x5168;&#x539F;&#x6837;&#x8F93;&#x51FA;&#xFF0C;&#x5C31;&#x7B97;&#x662F;&#x6709;&#x53D8;&#x91CF;&#xFF0C;&#x4E5F;&#x4E0D;&#x4F1A;&#x88AB;&#x89E3;&#x6790;&#x3002;</p>
<pre><code class="lang-shell">name=zhangsan
echo &apos;&#x5927;&#x5BB6;&#x597D;&#xFF0C;&#x6211;&#x662F;$name&apos;  # &#x5927;&#x5BB6;&#x597D;&#xFF0C;&#x6211;&#x662F;$name
</code></pre>
</li>
</ul>
<h2 id="&#x4E03;&#x3001;&#x811A;&#x672C;&#x91CC;&#x7684;">&#x4E03;&#x3001;&#x811A;&#x672C;&#x91CC;&#x7684;$</h2>
<ul>
<li><p>${var}&#x7528;&#x6765;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x503C;&#xFF0C;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5927;&#x62EC;&#x53F7;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x4E0D;&#x5199;&#x3002;</p>
<pre><code class="lang-shell">name=zhangsan
echo ${name}  # zhangsan
echo $name  # zhangsan
</code></pre>
</li>
<li><p>&#x7279;&#x6B8A;&#x53D8;&#x91CF;</p>
<pre><code class="lang-shell">$# &#x4EE3;&#x8868;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;
$@ &#x4EE3;&#x8868;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x7684;&#x5217;&#x8868;
$0 &#x4EE3;&#x8868;&#x811A;&#x672C;&#x672C;&#x8EAB;
$1 &#x4EE3;&#x8868;&#x4F20;&#x5165;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;$2&#xFF0C;$3...&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;
$* &#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x65B9;&#x5F0F;&#x663E;&#x793A;&#x6240;&#x6709;&#x4F20;&#x5165;&#x7684;&#x53C2;&#x6570;
$$ &#x811A;&#x672C;&#x8FD0;&#x884C;&#x7684;&#x8FDB;&#x7A0B;ID
$? &#x663E;&#x793A;&#x6700;&#x540E;&#x547D;&#x4EE4;&#x7684;&#x9000;&#x51FA;&#x72B6;&#x51B5;&#xFF0C;0&#x8868;&#x793A;&#x6CA1;&#x6709;&#x9519;&#x8BEF;
</code></pre>
</li>
<li><p>$(cmd)&#x7528;&#x6765;&#x6267;&#x884C;&#x62EC;&#x53F7;&#x91CC;&#x7684;&#x811A;&#x672C;&#x547D;&#x4EE4;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x4F7F;&#x7528;&#x53CD;&#x5F15;&#x53F7;&#x3002;</p>
<pre><code class="lang-shell">echo $(ls /home/chris/Desktop)  # &#x5217;&#x51FA;&#x5F53;&#x524D;&#x7528;&#x6237;&#x7684;&#x684C;&#x9762;&#x4E0A;&#x7684;&#x6240;&#x6709;&#x6587;&#x4EF6;&#x548C;&#x6587;&#x4EF6;&#x5939;
</code></pre>
</li>
<li><p>$((expression))&#x7528;&#x6765;&#x8BA1;&#x7B97;&#x6570;&#x5B66;&#x8868;&#x8FBE;&#x5F0F;expression&#x7684;&#x503C;&#x3002;</p>
<pre><code class="lang-shell">echo $((1+1))  # 2
</code></pre>
</li>
</ul>
<h2 id="&#x516B;&#x3001;&#x5206;&#x652F;&#x8BED;&#x53E5;-if">&#x516B;&#x3001;&#x5206;&#x652F;&#x8BED;&#x53E5;: <code>if</code></h2>
<p>&#x5206;&#x652F;&#x63A7;&#x5236;&#x8BED;&#x53E5;&#x5B8C;&#x6574;&#x683C;&#x5F0F;&#x4E3A;:</p>
<pre><code class="lang-bash"><span class="hljs-keyword">if</span> <span class="hljs-built_in">command</span>;<span class="hljs-keyword">then</span>
    commands
<span class="hljs-keyword">elif</span> <span class="hljs-built_in">command</span>;<span class="hljs-keyword">then</span>
    commands
<span class="hljs-keyword">else</span>
    commands
<span class="hljs-keyword">fi</span>
</code></pre>
<ol>
<li><p><code>if</code> &#x8BED;&#x53E5;&#x68C0;&#x67E5;&#x5224;&#x65AD;&#x7684;&#x4F9D;&#x636E;&#x5B9E;&#x9645;&#x4E0A;&#x662F;, &#x540E;&#x9762;&#x6240;&#x8DDF;&#x7684;&#x547D;&#x4EE4;&#x7684;&#x72B6;&#x6001;&#x7801;: 0 &#x4E3A; true, &#x5176;&#x4ED6;&#x503C; &#x4E3A; false</p>
<pre><code class="lang-bash"> <span class="hljs-keyword">if</span> ls /xxx;<span class="hljs-keyword">then</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&apos;exist xxx&apos;</span>
 <span class="hljs-keyword">else</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&apos;not exist xxx&apos;</span>
 <span class="hljs-keyword">fi</span>
</code></pre>
</li>
<li><p>&#x6761;&#x4EF6;&#x6D4B;&#x8BD5;&#x547D;&#x4EE4;: <code>[ ... ]</code></p>
<ul>
<li>shell &#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x4E13;&#x7528;&#x505A;&#x6761;&#x4EF6;&#x6D4B;&#x8BD5;&#x7684;&#x8BED;&#x53E5; <code>[ ... ]</code></li>
<li>&#x8FD9;&#x4E00;&#x5BF9;&#x65B9;&#x62EC;&#x53F7;&#x672C;&#x8D28;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;&#x547D;&#x4EE4;, &#x91CC;&#x9762;&#x7684;&#x6761;&#x4EF6;&#x662F;&#x5176;&#x53C2;&#x6570;, &#x6240;&#x4EE5; <code>[</code> &#x7684;&#x540E;&#x9762;&#x548C; <code>]</code> &#x7684;&#x524D;&#x9762;&#x5FC5;&#x987B;&#x6709;&#x7A7A;&#x683C;, &#x5426;&#x5219;&#x4F1A;&#x62A5;&#x9519;&#x3002;</li>
<li>&#x4ED6;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x4E09;&#x79CD;&#x6BD4;&#x8F83;<ul>
<li>&#x6570;&#x503C;&#x6BD4;&#x8F83;</li>
<li>&#x5B57;&#x7B26;&#x4E32;&#x6BD4;&#x8F83;</li>
<li>&#x6587;&#x4EF6;&#x6BD4;&#x8F83;</li>
</ul>
</li>
<li><p>&#x7528;&#x6CD5;:</p>
<pre><code class="lang-bash">  <span class="hljs-keyword">if</span> [ condition ];<span class="hljs-keyword">then</span>
      commands
  <span class="hljs-keyword">fi</span>
</code></pre>
</li>
</ul>
</li>
<li><p>&#x6761;&#x4EF6;&#x5217;&#x8868;</p>
<ul>
<li><p>&#x6570;&#x503C;&#x6BD4;&#x8F83;</p>
  <table>
    <tr>
      <th>Condition</th>
      <th>&#x8BF4;&#x660E;</th>
    </tr>
    <tr>
      <td>n1 -eq n2</td>
<td>&#x68C0;&#x67E5;n1&#x662F;&#x5426;&#x4E0E;n2&#x76F8;&#x7B49;</td>
    </tr>
    <tr>
      <td>n1 -ge n2</td>
      <td>&#x68C0;&#x67E5;n1&#x662F;&#x5426;&#x5927;&#x4E8E;&#x6216;&#x7B49;&#x4E8E;n2</td>
    </tr>
    <tr>
      <td>n1 -gt n2</td>
      <td>&#x68C0;&#x67E5;n1&#x662F;&#x5426;&#x5927;&#x4E8E;n2</td>
</tr>
    <tr>
      <td>n1 -le n2</td>
      <td>&#x68C0;&#x67E5;n1&#x662F;&#x5426;&#x5C0F;&#x4E8E;n2</td>
    </tr>
    <tr>
      <td>n1 -lt n2</td>
      <td>&#x68C0;&#x67E5;n1&#x662F;&#x5426;&#x5C0F;&#x4E8E;n2</td>
    </tr>
    <tr>
      <td>n1 -ne n2</td>
      <td>&#x68C0;&#x67E5;n1&#x662F;&#x5426;&#x4E0D;&#x7B49;&#x4E8E;n2</td>
    </tr>
  </table></li>
<li><p>&#x5B57;&#x7B26;&#x4E32;&#x6BD4;&#x8F83;</p>
  <table>
    <tr>
      <th>Condition</th>
      <th>&#x8BF4;&#x660E;</th>
    </tr>
    <tr>
      <td>str1 = str2</td>
  <td>&#x68C0;&#x67E5;str1&#x662F;&#x5426;&#x548C;str2&#x76F8;&#x540C;(&#x6CE8;&#x610F;&#xFF1A;&#x7B49;&#x53F7;&#x4E24;&#x8FB9;&#x8981;&#x52A0;&#x7A7A;&#x683C;)</td>
    </tr>
    <tr>
    </tr><tr>
      <td>str1!=str2</td>
      <td>&#x68C0;&#x67E5;str1&#x662F;&#x5426;&#x548C;str2&#x4E0D;&#x540C;</td>
    </tr>
    <tr>
      <td>str1&lt;str2</td>
      <td>&#x68C0;&#x67E5;str1&#x662F;&#x5426;&#x5C0F;&#x4E8E;str2(&#x9700;&#x8981;&#x4F7F;&#x7528;[[]])</td>
    </tr>
    <tr>
      <td>str1&gt;str2</td>
      <td>&#x68C0;&#x67E5;str1&#x662F;&#x5426;&#x5927;&#x4E8E;str2(&#x9700;&#x8981;&#x4F7F;&#x7528;[[]])</td>
    </tr>
    <tr>
      <td>-n str1</td>
      <td>&#x68C0;&#x67E5;str1&#x7684;&#x957F;&#x5EA6;&#x662F;&#x5426;&#x975E;0(&#x53D8;&#x91CF;&#x9700;&#x8981;&#x6DFB;&#x52A0;&#x53CC;&#x5F15;&#x53F7;)</td>
    </tr>
    <tr>
      <td>-z str1</td>
        <td>&#x68C0;&#x67E5;str1&#x7684;&#x957F;&#x5EA6;&#x662F;&#x5426;&#x4E3A;0</td>
    </tr>
  </table>
</li>
<li><p>&#x6587;&#x4EF6;&#x6BD4;&#x8F83;</p>
  <table>
    <tr>
      <th>Condition</th>
      <th>&#x8BF4;&#x660E;</th>
    </tr>
    <tr>
      <td>-d file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x662F;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;</td>
    </tr>
    <tr>
      <td>-e file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;</td>
    </tr>
    <tr>
      <td>-f file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x662F;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;</td>
    </tr>
    <tr>
      <td>-r file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x53EF;&#x8BFB;</td>
    </tr>
    <tr>
      <td>-w file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x53EF;&#x5199;</td>
    </tr>
    <tr>
      <td>-x file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x53EF;&#x6267;&#x884C;</td>
    </tr>
    <tr>
      <td>-s file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x975E;&#x7A7A;</td>
    </tr>
    <tr>
      <td>-O file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x5C5E;&#x4E8E;&#x5F53;&#x524D;&#x7528;&#x6237;&#x6240;&#x6709;</td>
    </tr>
    <tr>
      <td>-G file</td>
      <td>&#x68C0;&#x67E5;file&#x662F;&#x5426;&#x5B58;&#x5728;&#x5E76;&#x4E14;&#x6240;&#x5C5E;&#x7EC4;&#x4E0E;&#x5F53;&#x524D;&#x7528;&#x6237;&#x76F8;&#x540C;</td>
    </tr>
    <tr>
      <td>file1 -nt file2</td>
      <td>&#x68C0;&#x67E5;file1&#x662F;&#x5426;&#x6BD4;file2&#x65B0;</td>
    </tr>
    <tr>
      <td>file1 -ot file2</td>
      <td>&#x68C0;&#x67E5;file1&#x662F;&#x5426;&#x6BD4;file2&#x65E7;</td>
    </tr>
  </table>

</li>
</ul>
</li>
</ol>
<h1 id="&#x4E5D;&#x3001;case&#x8BED;&#x53E5;">&#x4E5D;&#x3001;case&#x8BED;&#x53E5;</h1>
<pre><code class="lang-shell">case  &quot;$var&quot; in
         exp1)
         command1
         ;;
         exp2)
         command2
         ;;
         exp3)
         command3
         ;;
         *)
         comman4
         ;;
esac
</code></pre>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-shell">#!/bin/bash
read -p &quot;&#x8BF7;&#x8F93;&#x5165;&#x6570;&#x5B57;&quot; num
case $num in
    0)
        echo &#x661F;&#x671F;&#x65E5;
        ;;
    1)
        echo &#x661F;&#x671F;&#x4E00;
        ;;
    2)
        echo &#x661F;&#x671F;&#x4E8C;
        ;;
    3)
        echo &#x661F;&#x671F;&#x4E09;
        ;;
    4)
        echo &#x661F;&#x671F;&#x56DB;
        ;;
    5)
        echo &#x661F;&#x671F;&#x4E94;
        ;;
    6)
        echo &#x661F;&#x671F;&#x516D;
        ;;
    *)
        echo &#x5176;&#x4ED6;
        ;;
esac
</code></pre>
<h2 id="&#x5341;&#x3001;&#x5FAA;&#x73AF;&#x8BED;&#x53E5;-for">&#x5341;&#x3001;&#x5FAA;&#x73AF;&#x8BED;&#x53E5;: for</h2>
<p>Shell &#x4E2D;&#x7684;&#x5FAA;&#x73AF;&#x7ED3;&#x6784;&#x6709;&#x4E09;&#x79CD;: <code>for</code>&#x3001;<code>while</code> &#x548C; <code>until</code>, &#x8FD9;&#x91CC;&#x91CD;&#x70B9;&#x4ECB;&#x7ECD; <code>for</code> &#x5FAA;&#x73AF;&#x3002;</p>
<ol>
<li><p>for &#x5FAA;&#x73AF;&#x7684;&#x57FA;&#x672C;&#x683C;&#x5F0F;:</p>
<pre><code class="lang-bash"> <span class="hljs-keyword">for</span> &#x53D8;&#x91CF; <span class="hljs-keyword">in</span> &#x5E8F;&#x5217;
 <span class="hljs-keyword">do</span>
     &#x8981;&#x6267;&#x884C;&#x7684;&#x547D;&#x4EE4;
 <span class="hljs-keyword">done</span>
</code></pre>
</li>
<li><p>&#x7EC3;&#x4E60;: &#x6253;&#x5370; 1 &#x5230; 10 &#x4E2D;&#x7684;&#x5076;&#x6570;</p>
<pre><code class="lang-bash"> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> `seq <span class="hljs-number">1</span> <span class="hljs-number">10</span>`
 <span class="hljs-keyword">do</span>
     <span class="hljs-keyword">if</span> [ $[ <span class="hljs-variable">$i</span> % <span class="hljs-number">2</span>] == <span class="hljs-number">0</span> ]
     <span class="hljs-keyword">then</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x5076;&#x6570;: <span class="hljs-variable">$i</span>&quot;</span>
     <span class="hljs-keyword">else</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x5947;&#x6570;: <span class="hljs-variable">$i</span>&quot;</span>
     <span class="hljs-keyword">fi</span>
 <span class="hljs-keyword">done</span>
</code></pre>
<ol>
<li><code>seq START END</code> &#x8BED;&#x53E5;&#x7528;&#x6765;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x5E8F;&#x5217;</li>
<li><code>$[ NUM1 + NUM2 ]</code> &#x8BED;&#x53E5;&#x7528;&#x6765;&#x8FDB;&#x884C;&#x57FA;&#x672C;&#x7684;&#x6570;&#x5B66;&#x8FD0;&#x7B97;</li>
<li><code>[[ ... ]]</code> &#x8BED;&#x53E5;&#x7528;&#x6765;&#x66F4;&#x65B9;&#x4FBF;&#x7684;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x5224;&#x65AD;</li>
</ol>
</li>
<li><p>C&#x8BED;&#x8A00;&#x98CE;&#x683C;&#x7684; for &#x5FAA;&#x73AF;</p>
<pre><code class="lang-bash"> <span class="hljs-keyword">for</span> ((i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">10</span>; i++))
 <span class="hljs-keyword">do</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;num is <span class="hljs-variable">$i</span>&quot;</span>
 <span class="hljs-keyword">done</span>
</code></pre>
</li>
</ol>
<h2 id="&#x5341;&#x4E00;&#x3001;&#x51FD;&#x6570;">&#x5341;&#x4E00;&#x3001;&#x51FD;&#x6570;</h2>
<ol>
<li><p>&#x51FD;&#x6570;&#x5B9A;&#x4E49;</p>
<p> &#x5B9A;&#x4E49;&#x65F6; <code>function</code> &#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565;</p>
<pre><code class="lang-bash"> <span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">foo</span></span>() {
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;---------------------------&quot;</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;Hello <span class="hljs-variable">$1</span>, nice to meet you!&quot;</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;---------------------------&quot;</span>
 }
</code></pre>
</li>
<li><p>&#x51FD;&#x6570;&#x7684;&#x4F7F;&#x7528;</p>
<ul>
<li>&#x5728;&#x7EC8;&#x7AEF;&#x6216;&#x811A;&#x672C;&#x4E2D;&#x76F4;&#x63A5;&#x8F93;&#x5165;&#x51FD;&#x6570;&#x540D;&#x5373;&#x53EF;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x5C0F;&#x62EC;&#x53F7;</li>
<li><p>&#x4F20;&#x53C2;&#x4E5F;&#x53EA;&#x9700;&#x5C06;&#x53C2;&#x6570;&#x52A0;&#x5230;&#x51FD;&#x6570;&#x540D;&#x540E;&#x9762;&#xFF0C;&#x4EE5;&#x7A7A;&#x683C;&#x505A;&#x95F4;&#x9694;&#xFF0C;&#x50CF;&#x6B63;&#x5E38;&#x4F7F;&#x7528;&#x547D;&#x4EE4;&#x90A3;&#x6837;</p>
<pre><code class="lang-bash">foo arg1 arg2 arg3 ...
</code></pre>
</li>
</ul>
</li>
<li><p>&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</p>
<pre><code class="lang-bash"> <span class="hljs-function"><span class="hljs-title">bar</span></span>() {
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x6267;&#x884C;&#x8005;&#x662F; <span class="hljs-variable">$0</span>&quot;</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x53C2;&#x6570;&#x6570;&#x91CF;&#x662F; <span class="hljs-variable">$#</span>&quot;</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x5168;&#x90E8;&#x7684;&#x53C2;&#x6570; <span class="hljs-variable">$@</span>&quot;</span>
     <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;&#x5168;&#x90E8;&#x7684;&#x53C2;&#x6570; $*&quot;</span>

     <span class="hljs-keyword">if</span> [ <span class="hljs-operator">-d</span> <span class="hljs-variable">$1</span> ]; <span class="hljs-keyword">then</span>  <span class="hljs-comment"># &#x68C0;&#x67E5;&#x4F20;&#x5165;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5426;&#x662F;&#x6587;&#x4EF6;&#x5939;</span>
         <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> `ls <span class="hljs-variable">$1</span>`
         <span class="hljs-keyword">do</span>
             <span class="hljs-built_in">echo</span> <span class="hljs-variable">$f</span>
         <span class="hljs-keyword">done</span>
     <span class="hljs-keyword">elif</span> [ <span class="hljs-operator">-f</span> <span class="hljs-variable">$1</span> ]; <span class="hljs-keyword">then</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">&quot;This is a file: <span class="hljs-variable">$1</span>&quot;</span>  <span class="hljs-comment"># &#x5982;&#x679C;&#x4E0D;&#x662F;&#x6587;&#x4EF6;&#x5939;, &#x76F4;&#x63A5;&#x663E;&#x793A;&#x6587;&#x4EF6;&#x540D;</span>
     <span class="hljs-keyword">else</span>
         <span class="hljs-built_in">echo</span> <span class="hljs-string">&apos;not valid&apos;</span>  <span class="hljs-comment"># &#x524D;&#x9762;&#x90FD;&#x4E0D;&#x5339;&#x914D;&#x663E;&#x793A;&#x9ED8;&#x8BA4;&#x8BED;&#x53E5;</span>
     <span class="hljs-keyword">fi</span>
 }
 bar <span class="hljs-variable">$1</span>
</code></pre>
</li>
</ol>
<h2 id="&#x5341;&#x4E8C;&#x3001;&#x6570;&#x7EC4;">&#x5341;&#x4E8C;&#x3001;&#x6570;&#x7EC4;</h2>
<p>&#x6570;&#x7EC4;&#x4E2D;&#x53EF;&#x4EE5;&#x5B58;&#x653E;&#x591A;&#x4E2A;&#x503C;&#x3002;Bash Shell &#x53EA;&#x652F;&#x6301;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;&#xFF08;&#x4E0D;&#x652F;&#x6301;&#x591A;&#x7EF4;&#x6570;&#x7EC4;&#xFF09;&#xFF0C;&#x521D;&#x59CB;&#x5316;&#x65F6;&#x4E0D;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x6570;&#x7EC4;&#x5927;&#x5C0F;&#x3002;</p>
<p>&#x4E0E;&#x5927;&#x90E8;&#x5206;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x7C7B;&#x4F3C;&#xFF0C;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x7684;&#x4E0B;&#x6807;&#x7531;0&#x5F00;&#x59CB;&#x3002;</p>
<ul>
<li>&#x5B9A;&#x4E49;&#x6570;&#x7EC4;&#xFF1A;</li>
</ul>
<p>Shell &#x6570;&#x7EC4;&#x7528;&#x62EC;&#x53F7;&#x6765;&#x8868;&#x793A;&#xFF0C;&#x5143;&#x7D20;&#x7528;&quot;&#x7A7A;&#x683C;&quot;&#x7B26;&#x53F7;&#x5206;&#x5272;&#x5F00;&#xFF0C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-shell">array_name=(value1 ... valuen)
</code></pre>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-shell">arr=(hello good &quot;yes&quot; a 12)
</code></pre>
<p>&#x540C;&#x65F6;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x4E0B;&#x6807;&#x6765;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x3002;</p>
<pre><code class="lang-shell">nums[0]=1
nums[1]=&quot;good&quot;
nums[2]=yes
</code></pre>
<ul>
<li>&#x83B7;&#x53D6;&#x548C;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#x6570;&#x636E;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x4E0B;&#x6807;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-shell">arr=(hello good &quot;yes&quot; a 12)
arr[0] = hi
</code></pre>
<p>&#x4F7F;&#x7528;$&#x548C;&#x4E0B;&#x6807;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4F46;&#x662F;&#x6CE8;&#x610F;&#x9700;&#x8981;&#x4F7F;&#x7528;{}&#x6765;&#x5305;&#x88F9;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x8BED;&#x6CD5;&#x683C;&#x5F0F;:</p>
<pre><code class="lang-shell">${arr[index]}
</code></pre>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-shell">arr=(hello good &quot;yes&quot; a 12)
echo ${arr[0]}
</code></pre>
<ul>
<li>&#x83B7;&#x53D6;&#x6570;&#x7EC4;&#x6240;&#x6709;&#x6570;&#x636E;&#x4EE5;&#x53CA;&#x957F;&#x5EA6;</li>
</ul>
<p>&#x4F7F;&#x7528;*&#x6216;&#x8005;@&#x4F5C;&#x4E3A;&#x4E0B;&#x6807;&#xFF0C;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x6570;&#x7EC4;&#x91CC;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-shell">arr=(hello good &quot;yes&quot; a 12)
echo ${arr[*]}
echo ${arr[@]}
</code></pre>
<p>&#x5728;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x6570;&#x636E;&#x7684;&#x524D;&#x9762;&#x6DFB;&#x52A0;# &#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5230;&#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;</p>
<pre><code class="lang-shell">arr=(hello good &quot;yes&quot; a 12)
echo ${#arr[*]}
echo ${#arr[@]}
</code></pre>
<ul>
<li><p>&#x6570;&#x7EC4;&#x7684;&#x904D;&#x5386;</p>
<ul>
<li>&#x4F7F;&#x7528;for&#x5FAA;&#x73AF;&#x904D;&#x5386;</li>
</ul>
<pre><code class="lang-shell">#!/bin/bash
arr=(hello good 12 &apos;yes&apos; hi)
for((i=0;i</code></pre>
<ul>
<li>&#x4F7F;&#x7528;for...in&#x904D;&#x5386;</li>
</ul>
<pre><code class="lang-shell">#!/bin/bash
arr=(hello good 12 &apos;yes&apos; hi)
for ele in ${arr[*]}
do
    echo $ele
done
</code></pre>
</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./14/section04.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 远端服务器的SSH连接"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Linux -->
<!-- End of book Linux -->
</html>
